package com.asnike.selector{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class CampItemBarList extends Sprite{
		
		private const ITEM_START_POS_X:Number = 15;
		private const ITEM_START_POS_Y:Number = 90;
		private const ITEM_GAP_HEIGHT:Number = 31;
		
		private var _items:Array = [];
		
		public function CampItemBarList(){
			super();
		}
		
		internal function addItem( $name:String, $price:int ):void{
			setItem( $name, $price );
			showItems();
		}
		private function setItem( $name:String, $price:int ):void{
			var item:CampItemBar;
			
			item = new CampItemBar;
			item.setItemName( $name );
			item.setPrice( $price );
			item.addEventListener( MouseEvent.CLICK, itemClick );
			
			_items[_items.length] = item;
		}
		private function itemClick( $e:MouseEvent ):void{
			trace( $e.currentTarget );
			removeItem( $e.currentTarget as CampItemBar );
		}
		private function showItems():void{
			var item:CampItemBar, index:int;
			
			
			for each( item in _items ){
				item.x = ITEM_START_POS_X;
				item.y = ITEM_START_POS_Y + ITEM_GAP_HEIGHT*index;
				addChild( item );
				++index;
			}
		}
		
		internal function removeItem( $target:CampItemBar ):void{
			removeChild( $target );
			_items.splice( _items.indexOf( $target ), 1 );
			showItems();
		}
		
		internal function getSelectedItems():Array{
			var i:int, j:int, result:Array;
			
			result = [];
			for( i = 0, j = _items.length ; i < j ; ++i ){
				result[result.length] = _items[i].getName();
			}
			return result;
		}
	}
}